import numpy as np


def cal_4f_fitness(coords):
    rounded_coords = np.round(coords).astype(int)
    # print("四舍五入后的数组：", rounded_coords)
    
    f1 = 0
    f2 = 0
    f3 = 0
    f4 = 0
    
    # -----f1-----
    w_bucket = [0] * (NUM_W)
    for x in rounded_coords:
        # 坐标的每一个分量，四舍五入以后 是多少，仓库id就是多少
        wid = x
        print("wid = ", wid)
        # print("this x = ", x, end=", ")
        w_bucket[wid] += 1
    
    for i in range(0,NUM_W):
        if ( w_bucket[i] > 0 ):
            # print("给f1加上", data_warehouse[i]['daily_cost'])
            f1 += data_warehouse[i]['daily_cost']
            
    print("f1 = ", f1)
    
    # -----f2 f3-----
    sum_inventory_all_w = [0] * (NUM_W) 
    sum_sales_all_w = [0] * (NUM_W)          
    
    # for x in rounded_coords:
    for idx, val in enumerate(rounded_coords):
        cid = idx
        wid = val
        sum_inventory_all_w[wid] += data_category_inventory[cid]['average_inventory']
        sum_sales_all_w[wid]     += data_category_sales[cid]['average_sales']

    for i in range(0,NUM_W):
        f2 += utilization_score( sum_inventory_all_w[i] / data_warehouse[i]['max_inventory'] )
        f3 += utilization_score( sum_sales_all_w[i]     / data_warehouse[i]['max_sales']     )
    
    print("f2 = ", f2)
    print("f3 = ", f3)
    # -----f4-----
    
    cinw_mx = [[] for _ in range(NUM_W)]
    for idx, val in enumerate(rounded_coords):
        cid = idx
        wid = val
        cinw_mx[wid].append(cid)
        
    for row_index, row in enumerate(cinw_mx):
        print("row idx={},: {}".format(row_index, row) )
        len_thisw = len(row)
        for i in range(len_thisw):
            for j in range(len_thisw):
                x = row[i]
                y = row[j]
                print("x = {}, y = {}".format(x, y), end=", ")
                print("mx[x][y] = ", mx_category_association[x][y])
                f4 += mx_category_association[x][y]
    
    # print("f4 = ", f4)
     
    return f1,f2,f3,f4, f1*f2*f3*f4